package com.example.medical_delivery_system.service.impl;

import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.dao.AdminProductMapper;
import com.example.medical_delivery_system.entity.Product;
import com.example.medical_delivery_system.service.AdminProductService;
import com.example.medical_delivery_system.util.PageQueryUtil;
import com.example.medical_delivery_system.util.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Param：20201003013
 * Author：李镇荣
 * Data：2022/12/21
 */
@Service
public class AdminProductServiceImpl implements AdminProductService {

    private AdminProductMapper productMapper;


    @Override
    public PageResult getProductPage(PageQueryUtil pageUtil) {
        List<Product> products = productMapper.findProductList(pageUtil);
        int total = productMapper.getTotalProducts(pageUtil);
        PageResult pageResult = new PageResult(products, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveProduct(Product product) {
        if (productMapper.insertSelective(product) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateProduct(Product product) {
        Product temp = productMapper.selectByPrimaryKey(product.getProductId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        temp.setProductName(product.getProductName());
        temp.setDetails(product.getDetails());
        temp.setPrice(product.getPrice());
        if (productMapper.updateByPrimaryKeySelective(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除数据
        return productMapper.deleteBatch(ids) > 0;
    }
}
